package demo.shardingspere.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import demo.shardingspere.domain.Order;
import demo.shardingspere.domain.User;
import demo.shardingspere.mapper.UserMapper;
import demo.shardingspere.service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author: 学相伴-飞哥
 * @description: UserController
 * @Date : 2021/3/10
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TOrderService orderService;


    @GetMapping("/save")
    public String insert(String birthday) {
        User user = new User();
        user.setNickname("zhangsan" + new Random().nextInt());
        user.setPassword("1234567");
        user.setSex(new Random().nextInt(2));
        user.setBirthday(new Date());
        user.setAge(new Random().nextInt(100));
        userMapper.insert(user);
        Order order = new Order();
        order.setUserid(user.getId());
        order.setCreateTime(new Date());
        order.setOrdernumber("133455678");
        order.setProductid(1234L);
        orderService.save(order);
        return "success";
    }


    @GetMapping("/listuser")
    public List<User> listuser() {
        return userMapper.selectList(new QueryWrapper());
    }
}